package com.ymlyj666.onlinechat.control.friends;

import com.alibaba.fastjson.JSON;
import com.ymlyj666.onlinechat.dao.FriendShipDao;
import com.ymlyj666.onlinechat.dao.UserDao;
import com.ymlyj666.onlinechat.model.FriendShip;
import com.ymlyj666.onlinechat.model.Response;
import com.ymlyj666.onlinechat.model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/friends/list.do")
public class ListServlet extends HttpServlet {
    private UserDao userDao;
    private FriendShipDao friendShipDao;

    @Override
    public void init() throws ServletException {
        super.init();
        userDao = new UserDao();
        friendShipDao = new FriendShipDao();
    }

    @Override
    public void destroy() {
        try {
            userDao.close();
            friendShipDao.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.destroy();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = req.getParameter("op");
        String uidFriend = req.getParameter("uid_friend");
        if (op == null) {
            op = "";
        }

        HttpSession session = req.getSession();
        User currentUser = (User) session.getAttribute("user");

        switch (op) {
            case "add": {
                User friend = userDao.findOne(uidFriend);
                FriendShip friendShip = new FriendShip();
                friendShip.setUidHost(currentUser.getUid());
                friendShip.setUidFriend(uidFriend);
                friendShip.setAccepted(true);
                friendShip.setMemoName(friend.getUsername());
                friendShipDao.save(friendShip);

                FriendShip f = new FriendShip();
                f.setUidFriend(currentUser.getUid());
                f.setUidHost(uidFriend);
                f.setMemoName(currentUser.getUsername());
                f.setAccepted(false);
                friendShipDao.save(f);
            }
            break;
            case "accept": {
                FriendShip f1 = friendShipDao.findByUidPair(currentUser.getUid(), uidFriend);
                if (f1 != null) {
                    f1.setAccepted(true);
                    friendShipDao.save(f1);
                }
            }
            break;
            case "delete": {
                friendShipDao.deleteByUidPair(uidFriend, currentUser.getUid());
                friendShipDao.deleteByUidPair(currentUser.getUid(), uidFriend);
            }
            break;
            case "changeMemoName": {
                String newMemoName = req.getParameter("new_memo_name");
                FriendShip friendShip = friendShipDao.findByUidPair(currentUser.getUid(), uidFriend);
                friendShip.setMemoName(newMemoName);
                friendShipDao.save(friendShip);
            }
            break;
            default:
                break;
        }

        List<FriendShip> friendShips = friendShipDao.findByUidHost(currentUser.getUid());
        friendShips.addAll(friendShipDao.findByUidFriendAndNotAccepted(currentUser.getUid()));

        resp.getWriter().print(JSON.toJSONString(new Response<>(friendShips)));
    }
}
